package com.letfox.frm.controller.frm;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.letfox.common.enums.ResponseEnum;
import com.letfox.common.model.ResultResponse;
import com.letfox.frm.controller.BaseController;
import com.letfox.frm.service.UploadService;

@Controller
@RequestMapping(value = "/frm")
public class IndexController extends BaseController {
	
	@Autowired
	private UploadService uploadService;

	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@RequestMapping(value = "/index",method=RequestMethod.POST)
	@ResponseBody
	public ResultResponse index(){
		try {
			resultResponse.setStatusCode(ResponseEnum.R200.getCode());
			logger.info("#######################");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			ResultResponse.getFailInstance(e);
		}
		return resultResponse;
	}
	
	@RequestMapping(value="/upload",method=RequestMethod.POST)
	@ResponseBody
	public ResultResponse fileUpload(@RequestParam(value = "filter") MultipartFile[] filter){
       	try {      	
    		//文件路径
    		String path = request.getSession().getServletContext().getRealPath("/");	
    		resultResponse=uploadService.uploadFile(filter, path);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
       	return resultResponse;
	}

	@RequestMapping(value="/download",method=RequestMethod.POST)
	public ResponseEntity<byte[]> downloadFile(@RequestParam("path") String path){
		ResponseEntity<byte[]> responseEntity=null;
		try {
			responseEntity=uploadService.downloadFile(path);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return responseEntity;
	}
}
